// 设置请求根目录
axios.defaults.baseURL = 'http://ajax-api.itheima.net'
// axios.defaults.baseURL = 'https://hmajax.itheima.net/'


// 设置请求头
axios.interceptors.request.use(config => {
    // Do something before request is sent
    config.headers.Authorization = localStorage.getItem('homework-token')
    return config;
}, error => {
    // Do something with request error
    return Promise.reject(error);
});




axios.interceptors.response.use(response => {
    // Do something before response is sent
    // 未携带token，或token过期，后台都会返回401
    // console.log(response.status);
    if (response.status === 401) {
        location.href = '/login.html'
        localStorage.removeItem('homework-token')
        localStorage.removeItem('homework-username')
    }
    return response;
}, error => {
    // Do something with response error
    return Promise.reject(error);
});